Python For Beginners: Python Programming, Computer Programming by Robert Hill
Author:Robert Hill
Language: eng
Format: azw3
Published: 2017-08-14T07:00:00+00:00
age = input(“How old are you : ”)
if age >= 18:
print(”You can enter this website!”)
if age < 18:
Print(“There is nothing to see!”)
In the first line we create variable but you don’t know what input() function means, let’s quickly explain. The input() function prints value of what’s inside brackets and waits user to type something and press [Enter], and then stores information in variable. In this case variable age stored age of user. Then the if statement checked if age is greater than or equal to 18, if [age >= 18] returned True, if statement goes and runs what’s under it, in our case [print(“You can enter this website”)], if returned False if statement skips code under it. The same on the next line, if statement checks if age is less than 18 and if it’s True runs under code if False skips, but you might notice that here if statement uses two equal sign, this is because in programming equal sign means defining variables and there will be an error, so remember always use two equal sign instead one equal sign. You can test this simple example program, and it should work.
if – Else
Now let’s write simple password program that will check if password that entered by user is correct or not. To write this program you can use only if statement but in some cases it’s not comfortable. Let’s see how to write simple password program by if statement and “!=”. Open new file and type the following:
#Password Checker
Password = input(“Please Enter The Password : ”)
if Password == “1234567890”:
print(“Access Granted!”)
if Password != “1234567890”:
print(“Access Denied!”)
If you run this program, you will see that program asks you password and then program will check if the password is correct or not and writes the answer on screen. The “!=” in Python means “not equal to”, but now let’s see other way how you can write this program. Open new file and type the following:
#Password Checker With Else
Password = input(“Please Enter The Password : ”)
if Password == “1234567890”:
print(“Access Granted!”)
else:
print(“Access Denied!”)
So in this program everything is the same, but here we used “else”. Basically what else statement does, is that checks if the if statement done, goes and does code under it, it did not do, skips code and continues.
If-elif-else
There is also an “else if” statement called the elif statement. Like an if statement, it has a condition. Like an else statement, it follows an if (or another elif) statement and executes if the previous if (or elif) statement’s condition was False. You can read if, elif and else statements as, “If this condition is true, run this block. Or else, check if this next condition is true. Or else, just run this last block”. Open new file type the following and run by pressing F5:
#if-elif-else
number_of_eggs = 12
if number_of_eggs< 5:
print(“That is not too many eggs!”)
elifnumber_of_eggs< 20:
print(“You have quit few eggs!”)
elifnumber_of_eggs> 100:
print(“You have a lot of eggs. Gross!”)
else:
print(“You have not have any egg!”)
In this chapter you learned what is if, elif and else statements, in the next chapter we will understand what is loops and how to use them.
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Deep Learning with Python by François Chollet(12585)
Hello! Python by Anthony Briggs(9921)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(9799)
The Mikado Method by Ola Ellnestam Daniel Brolund(9782)
Dependency Injection in .NET by Mark Seemann(9345)
A Developer's Guide to Building Resilient Cloud Applications with Azure by Hamida Rebai Trabelsi(9306)
Hit Refresh by Satya Nadella(8826)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(8305)
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(7787)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(7769)
Grails in Action by Glen Smith Peter Ledbrook(7700)
The Kubernetes Operator Framework Book by Michael Dame(7672)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7563)
Exploring Deepfakes by Bryan Lyon and Matt Tora(7463)
Practical Computer Architecture with Python and ARM by Alan Clements(7384)
Implementing Enterprise Observability for Success by Manisha Agrawal and Karun Krishnannair(7367)
Robo-Advisor with Python by Aki Ranin(7342)
Building Low Latency Applications with C++ by Sourav Ghosh(7247)
Svelte with Test-Driven Development by Daniel Irvine(7213)
